package com.wing.product.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.UniqueIdGeneratorUtil;
import com.wing.product.dao.WarehouseFeeItemsDao;
import com.wing.product.model.entity.WarehouseFeeItems;
import com.wing.product.model.entity.WarehouseStorageOrder;
import com.wing.product.model.request.WarehouseFeeItemsForm;
import com.wing.product.model.response.WarehouseFeeItemsVO;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class WarehouseFeeItemsServiceImpl extends ServiceImpl<WarehouseFeeItemsDao, WarehouseFeeItems> implements WarehouseFeeItemsService{

    public Page<WarehouseFeeItemsVO> pageList4app(Page page, Map<String, Object> params) {
        List<WarehouseFeeItemsVO> list = baseMapper.list4app(page, params);
        page.setRecords(list);
        return page;
    }

    @Override
    public JsonResult saveWarehouseFeeItems(WarehouseFeeItemsForm warehouseFeeItemsForm){
        if (ObjectUtils.isNotEmpty(warehouseFeeItemsForm)){
            WarehouseFeeItems warehouseFeeItems = new WarehouseFeeItems();
            CopyOptions copyOptions = CopyOptions.create()
                    .setEditable(WarehouseFeeItems.class)
                    .setIgnoreError(true)
                    .setIgnoreNullValue(true);
            BeanUtil.copyProperties(warehouseFeeItemsForm, warehouseFeeItems, copyOptions);
//            warehouseFeeItems.setFeeItemCode(UniqueIdGeneratorUtil.generateUniqueId("SFX", "yyyyMMddHHmmss", 1000, 9999));
            this.save(warehouseFeeItems);
            return JsonResult.success("添加成功");
        }else {
            return JsonResult.fail("添加失败");
        }
    }

    @Override
    public JsonResult updateWarehouseFeeItems(WarehouseFeeItemsForm warehouseFeeItemsForm){
        if (ObjectUtils.isNotEmpty(warehouseFeeItemsForm)){
            WarehouseFeeItems warehouseFeeItems = new WarehouseFeeItems();
            CopyOptions copyOptions = CopyOptions.create()
                    .setEditable(WarehouseFeeItems.class)
                    .setIgnoreError(true)
                    .setIgnoreNullValue(true);
            BeanUtil.copyProperties(warehouseFeeItemsForm, warehouseFeeItems, copyOptions);
            this.baseMapper.updateById(warehouseFeeItems);
            return JsonResult.success("修改成功");
        }else {
            return JsonResult.fail("修改失败");
        }
    }

    @Override
    public WarehouseFeeItems selectFeeItems(String id){
       return this.baseMapper.selectById(id);
    }

    @Override
    public JsonResult deleteFeeItems(String id,String status){
        LambdaUpdateWrapper<WarehouseFeeItems> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(WarehouseFeeItems::getId, id)
                .set(WarehouseFeeItems::getStatus,status);
        this.update(lambdaUpdateWrapper);
        return JsonResult.success("操作成功");
    }
}
